package mysgoog.util;

import java.io.*;
/**
* @author Fu, Zhongyang
*/
public class FileManager
{

    public static String read(String filename)
    {
        StringBuffer contents = new StringBuffer();
        BufferedReader reader = null;
        try
        {
            File file = new File(filename);
            reader = new BufferedReader(new FileReader(file));
            String text = null;
            while ((text = reader.readLine()) != null)
            {
                contents.append(text).append(System.getProperty("line.separator"));
            }
        } catch (FileNotFoundException e)
        {
            e.printStackTrace();
        } catch (IOException e)
        {
            e.printStackTrace();
        } finally
        {
            try
            {
                if (reader != null)
                {
                    reader.close();
                }
            } catch (IOException e)
            {
                e.printStackTrace();
            }
        }
        return contents.toString();
    }

    public static void write(String filename, String content)
    {
        FileOutputStream fos = null;
        BufferedWriter bw = null;
        String encoding = "utf-8";
        try
        {
            File file = new File(filename);
            fos = new FileOutputStream(file);
            bw = new BufferedWriter(new OutputStreamWriter(fos, encoding));
            bw.write(content);
        } catch (FileNotFoundException fnfe)
        {
            fnfe.printStackTrace();
        } catch (IOException ioe)
        {
            ioe.printStackTrace();
        } finally
        {
            try
            {
                if (bw != null)
                    bw.close();
                if (fos != null)
                    fos.close();
            } catch (IOException ie)
            {
            }
        }
    }
    public static void newFolder(String folderPath)
    {
        String filePath = folderPath;
        filePath = filePath.toString();
        java.io.File myFilePath = new java.io.File(filePath);
        try
        {
            if (myFilePath.isDirectory())
            {
                System.err.println("the directory is exists!");
            } else
            {
                myFilePath.mkdir();
                System.err.println("create successfully");
            }
        } catch (Exception e)
        {
            System.err.println("error");
            e.printStackTrace();
        }
    }

}
